﻿#region License
/*
 *CopyRight (c) By Denuvead All Rights Reserved
 *CreateDate:
 *Info:
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using System.Windows.Forms;
using System.Reflection;
using PriErp.Tools.Extensions;
using DevComponents.DotNetBar.SuperGrid;
using System.Xml;

namespace PriErp.Commands
{
    public static class ReginsCommand
    {
        /// <summary>
        /// 显示指定功能模块
        /// </summary>
        /// <param name="sTabCtlMain"></param>
        /// <param name="view"></param>
        /// <param name="mHandler"></param>
        /// <returns></returns>
        public static UserControl ShowViewPal(SuperTabControl sTabCtlMain, ViewNames view, MouseEventHandler mHandler)
        {
            UserControl item = null;
            string title = view.GetDescription();
            if (ReginsCommand.NeedOpenTab(sTabCtlMain, title))
            {
                SuperTabItem tabItem = sTabCtlMain.CreateTab(title);
                tabItem.MouseUp += new MouseEventHandler(mHandler);
                SuperTabControlPanel tcp = new SuperTabControlPanel();
                tabItem.AttachedControl = tcp;
                tcp.TabItem = tabItem;
                tcp.Dock = DockStyle.Fill;

                string sCtl = string.Format("PriErp.Frm.{0}Ctl", view);
                Assembly asmb = Assembly.LoadFrom("PriErp.exe");
                Type type = asmb.GetType(sCtl);
                ConstructorInfo c = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
                item = c.Invoke(new Object[] { }) as UserControl;

                item.Dock = System.Windows.Forms.DockStyle.Fill;
                tcp.Controls.Add(item);
                sTabCtlMain.Controls.Add(tcp);
                sTabCtlMain.SelectedTab = tabItem;
            }
            return item;
        }
       /// <summary>
        /// 检查SuperTabControl中Tab是否需要打开
       /// </summary>
        /// <param name="sTabCtlMain">指定SuperTabControl</param>
       /// <param name="tabName">Tab名称</param>
       /// <returns>是否需要打开</returns>
        public static bool NeedOpenTab(SuperTabControl sTabCtlMain,string tabName)
        {
            bool NeedOpened = true;

            foreach (SuperTabItem tab in sTabCtlMain.Tabs)
            {
                if (tab.Text.Trim() == tabName.Trim())
                {
                    NeedOpened = false;
                    sTabCtlMain.SelectedTab = tab;
                    break;
                }
            }
            return NeedOpened;
        }

        /// <summary>
        /// SuperTab右键关闭功能
        /// </summary>
        /// <param name="sTabCtlMain">指定的SuperTab</param>
        /// <param name="sCurrent">当前SuperTabItem</param>
        /// <param name="parm">关闭参数</param>
        public static void TabCloseCommand(SuperTabControl sTabCtlMain,SuperTabItem sCurrent,string parm)
        {
            if (parm == "Current")
                sTabCtlMain.Tabs.Remove(sCurrent);
            else
            {
                int iCount = sTabCtlMain.Tabs.Count - 1;
                for (; iCount >= 0; iCount--)
                {
                    if (sTabCtlMain.Tabs[iCount] is SuperTabItem)
                    {
                        SuperTabItem item = sTabCtlMain.Tabs[iCount] as SuperTabItem;
                        if (parm == "Other" && item == sCurrent)
                            continue;
                        else if (item.CloseButtonVisible)
                            sTabCtlMain.Tabs.Remove(item);
                    }
                }
            }
        }

        /// <summary>
        /// 初始化SuperGrid列
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="ColumnList"></param>
        /// <param name="showCheck"></param>
        /// <param name="showIndex"></param>
        public static void InitGridColumns(SuperGridControl grid, XmlNodeList ColumnList,bool showCheck,bool showIndex)
        {
            grid.PrimaryGrid.AutoGenerateColumns = false;

            if (showCheck)
            {
                GridColumn selectCol = new GridColumn("选择");
                selectCol.Width = 40;
                selectCol.ColumnSortMode = ColumnSortMode.None;
                selectCol.EditorType = typeof(GridCheckBoxXEditControl);
                selectCol.ResizeMode = ColumnResizeMode.None;
                selectCol.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;
                grid.PrimaryGrid.Columns.Add(selectCol);
            }

            if (showIndex)
            {
                GridColumn indexCol = new GridColumn("序号");
                indexCol.ColumnSortMode = ColumnSortMode.None;
                indexCol.ResizeMode = ColumnResizeMode.None;
                indexCol.Width = 60;
                indexCol.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;
                grid.PrimaryGrid.Columns.Add(indexCol);
            }

            int iCount = ColumnList.Count;
            for (int i = 0; i < iCount; i++)
            {
                XmlNode ColumnNode = ColumnList[i];
                GridColumn gridColumn = new GridColumn();
                gridColumn.Name = ColumnNode.InnerTextOrEmpty();
                gridColumn.ColumnSortMode = ColumnNode.Attributes["SortMode"].ValueOrEmpty().ToEnum<ColumnSortMode>();
                gridColumn.CellStyles.Default.Alignment = ColumnNode.Attributes["Align"].ValueOrEmpty().ToEnum<DevComponents.DotNetBar.SuperGrid.Style.Alignment>();
                int iWidth = ColumnNode.Attributes["Width"].ValueOrEmpty().ToInt32();
                if (iWidth > 0)
                {
                    gridColumn.Width = iWidth;
                    gridColumn.AutoSizeMode = ColumnAutoSizeMode.None;
                }
                else
                    gridColumn.AutoSizeMode = ColumnAutoSizeMode.Fill;
                gridColumn.Visible = ColumnNode.Attributes["Visible"].ValueOrEmpty() != "0";
                gridColumn.DataPropertyName = ColumnNode.Attributes["Col"].ValueOrEmpty();
                grid.PrimaryGrid.Columns.Add(gridColumn);
            }
        }
    }
}
